MVC - JSP Servlet
Using Java Beans and Servlets together is the foundation of Java web development. This combination typically implements the MVC (Model-View-Controller) design pattern:
The Java Bean ( The Model):
A Java Bean is simply a Java class that follows specific rules: it must have a no-argument constructor, private properties, and public getters/setters. Store this bean in webapps/myapp/WEB-INF/com/example.
Compile that java bean there, such that the class file is also stored there.
package com.example;
import java.io.Serializable;
public class UserBean implements Serializable {
private String firstName;
private String email;
public UserBean() {}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
The Servlet (The Controller):
The Servlet receives the data from the web browser, creates an instance of the Java Bean, populates it, and passes it to the next page.
Store this sevlet in folder: webapps/myapp/WEB-INF/classes/com/example. Compile that
servlet such that the class file is also stored in the same folder.
C:\apache-tomcat\webapps\myapp\WEB-INF\classes\com\example\>javac -cp "C:\apache-tomcat\lib\jakarta.servlet-api-6.0.0.jar" UserServlet.java
package com.example;
import com.example.UserBean;
import java.io.IOException;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
public class UserServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String nameInput = request.getParameter("username");
String emailInput = request.getParameter("useremail");
UserBean user = new UserBean();
user.setFirstName(nameInput);
user.setEmail(emailInput);
request.setAttribute("currentUser", user);
request.getRequestDispatcher("profile.jsp").forward(request, response);
}
}
The HTML Form (The Input):
This is the entry point where the user types their information. Note the action matches the Servlet annotation.
Save this HTML form inside webapps\myapp folder.
<h2>Registration Form</h2>
<form action="register" method="post">
Name: <input type="text" name="username"><br>
Email: <input type="text" name="useremail"><br>
<input type="submit" value="Register">
</form>
The View (JSP) The JSP picks up the Java Bean from the request and displays the data. Save this jsp file inside webapps\myapp folder.
profile.jsp
<h2>Registration Successful</h2>
<p>Welcome, <strong>${currentUser.firstName}</strong>!</p>
<p>We will send updates to: ${currentUser.email}</p>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee
https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd"
version="6.0">
<servlet>
<servlet-name>UserServlet</servlet-name>
<servlet-class>com.example.UserServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UserServlet</servlet-name>
<url-pattern>/register</url-pattern>
</servlet-mapping>
</web-app>